﻿using MediatR;
using ProcurementManagement.Applcation.Command.QualityCommand;
using ProcurementManagement.Domain.Dtos.Quality;
using ProcurementManagement.Domain.ProcurementDomain;
using ProcurementManagement.Infrstructre.Interface;
using SqlSugar;

namespace ProcurementManagement.Applcation.CommandHanlers.QualityCommandHandler
{
    /// <summary>
    /// 反填
    /// </summary>
    public class FillProApplyCommandHandlerss:IRequestHandler<FillProApplyCommand,SelectProPcApplyDTO>
    {


        private readonly IProPcApplyRepository repository;
        private readonly IPurchaseContractsRepository _purchaseContractsRep;
        private readonly ISourcProductContractRepository _sourceProductContractRep;
        private readonly InterfaceVendorRepository _vendorRep;

        ISqlSugarClient db;
        

        public FillProApplyCommandHandlerss( IProPcApplyRepository repository, IPurchaseContractsRepository purchaseContractsRep, ISourcProductContractRepository sourceProductContractRep, InterfaceVendorRepository vendorRep, ISqlSugarClient db)
        {
            this.repository = repository;
            this._purchaseContractsRep = purchaseContractsRep;
            this._sourceProductContractRep = sourceProductContractRep;
            this._vendorRep = vendorRep;
            this.db = db;
        
        }

    

        async Task<SelectProPcApplyDTO> IRequestHandler<FillProApplyCommand, SelectProPcApplyDTO>.Handle(FillProApplyCommand request, CancellationToken cancellationToken)
        {
            var list = db.Queryable<ProPcApply, PurchaseContracts, SourcProductContract, Vendor>((pq, pc, sp, Vendor) => new JoinQueryInfos(JoinType.Inner, pq.PurchaseContractsIds == pc.PurchaseContractsIds, JoinType.Inner, pc.PCNumber == sp.PCNumber, JoinType.Inner, pc.VendorId == Vendor.VendorId)).Where((pq, pc, sp, Vendor) => pq.IsDel == false).Select((pq, pc, sp, Vendor) => new SelectProPcApplyDTO
            {
                ProApplyId = pq.ProApplyId,
                PurchaseContractsIds = pc.PurchaseContractsIds,
                ProcureInspectionId = pq.ProcureInspectionId,
                ManpowerId = pq.ManpowerId,
                ProContract = pc.PCName,
                QcTopic = pq.QcTopic,
                QcCode = pq.QcCode,
                QcPerson = pq.QcPerson,
                QcTime = pq.QcTime,
                SPCNum = sp.SPCNum,
                SPCPrice = sp.SPCPrice,
                SPCTotalPrice = sp.SPCAfterPrice,
                SPCArrivalDate = sp.SPCArrivalDate,
                VName = Vendor.VName,
                AddName = pq.AddName,
                AddTime = pq.AddTime,
                IsDel = pq.IsDel,
                SPPName = sp.SPPName,
                SPPNumber = sp.SPPNumber,
                SPPSpec = sp.SPPSpec

            });

            return  list.First();
        }
    }
}
